﻿@model dynamic
@using Kooboo.CMS.Web

@Html.IncludeLocalizationScripts()
<script>
    (function ($) {
        //#region Initialize the js culture
        Globalize.culture('@Html.Raw(System.Threading.Thread.CurrentThread.CurrentCulture.Name)');

        $.validator.methods.number = function (value, element) {
            return (!$.validator.methods.required.call(this, value, element) && "dependency-mismatch") || !isNaN(Globalize.parseFloat(value));
        }
        $.fn.numberic = function () {
            var $self = $(this) @*use in control type*@
            $self.find('input[data-value-type=float]').numeric('@Html.Raw(System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator)');
            $self.find('input[data-value-type=int32]').numeric(false);
        };
    })(jQuery);
</script>

@{
    var route = ViewContext.RequestContext.AllRouteValues().Merge("search", null);
    var uiCulture = System.Threading.Thread.CurrentThread.CurrentUICulture;
    var tinymceLang = "en";
    var tinymceLangFile = Server.MapPath(string.Format("~/Scripts/tiny_mce/langs/{0}.js", uiCulture.Name));
    if (File.Exists(tinymceLangFile))
    {
        tinymceLang = uiCulture.Name.ToLower();
    }
    string tinymceContentStyle = null;
    if (Kooboo.CMS.Sites.Models.Site.Current != null)
    {
        tinymceContentStyle = (new FrontUrlHelper(Url, Site.Current, Kooboo.CMS.Sites.Web.FrontRequestChannel.Debug)).FileUrl("tinymce_content.css", false).ToString();
        if (!File.Exists(Server.MapPath(tinymceContentStyle)))
        {
            tinymceContentStyle = null;
        }
    }

    var mediaLibraryUrl = Url.Action("Selection", "MediaContent", route.Merge("Area", "Contents").Merge("FolderName", "").Merge("listType", "grid").Merge("SingleChoice", "true"));
}

<script type="text/javascript">
    $(function () {
        window.leaveConfirm.bind('@("All your changes might be lost.".Localize())');
        //$(window.body).numberic();
        var mediaLibraryUrl = '@Html.Raw(mediaLibraryUrl)';

        var chooseFileFromMediaLibrary = function (inputId, value, fileType, window) {

            // execute popup
            var topJQ = top._jQuery || top.jQuery;
            var id = new Date().getTime();
            topJQ.pop({
                id: id,
                url: mediaLibraryUrl,
                width: 900,
                height: 500,
                dialogClass: 'iframe-dialog',
                frameHeight: '100%',
                beforeLoad: function () {
                },
                onload: function (handle, pop, config) {
                    top.onFileSelected = function (src, text, option) {
                        var $srcInput = topJQ('#' + inputId);
                        if ($srcInput.length == 0) {
                            $srcInput = $('#' + inputId);
                        }
                        $srcInput.val(src);
                        var $descriptionInput = $srcInput.parent().parent().parent().next().find('input');
                        $descriptionInput.val(text);
                    };
                    top.fileSelectPop = pop;
                },
                onclose: function (handle, pop, config) {

                }
            });
            tinymce.ztopKoobooDialog(id);
        }

        //#endregion
        var hasTinymce = false;
        $('textarea.tinymce').each(function () {
            var textarea = $(this);
            var tinyMCEConfig = $.extend({}, tinymce.getKoobooConfig(), {
                '$textarea': textarea,
                'elements': textarea.attr('id'),
                file_browser_callback: chooseFileFromMediaLibrary,
                'media_library_url': mediaLibraryUrl,
                @if (!string.IsNullOrEmpty(tinymceContentStyle))
                {
                    <text>content_css: '@(tinymceContentStyle)',</text>
                }
                language: '@tinymceLang',
                setup: function (ed) {
                    ed.on('change', function (ed, l) {
                        window.leaveConfirm.stop();
                    });
                    ed.on('FullscreenStateChanged', function (e) {
                        $(window.parent.document).find('iframe').toggleClass('fullscreen');
                    });
                    ed.on('BeforeSetContent', function (e) {
                        e.format = 'raw';
                    });
                }
            });

            tinyMCE.init(tinyMCEConfig);

            hasTinymce = true;
        });
        if (hasTinymce) {
            window.ajaxFormParam = {
                beforeSerialize: function ($form, options) {
                    tinyMCE.triggerSave();
                }
            };
        }

        $('textarea.codemirror').each(function () {
            $(this).codeMirror();
        });
    });
</script>
